package web_thms_mapper

import (
	"context"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"sync"
	"transport/web/web_thms_model"
	"transport/web/web_vo"
)

type ThmsMapper struct {
	DB   *gorm.DB
	Ctx  context.Context
	Lock *sync.RWMutex
}

func (mapper *ThmsMapper) Create(m *web_thms_model.YtlThms) (ret *web_thms_model.YtlThms, err error) {
	mapper.Lock.Lock()
	defer mapper.Lock.Unlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Create(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *ThmsMapper) Upsert(m *web_thms_model.YtlThms) (ret *web_thms_model.YtlThms, err error) {
	if err = mapper.DB.WithContext(mapper.Ctx).Clauses(clause.OnConflict{
		Columns: []clause.Column{{Name: "name"}},
		DoUpdates: clause.AssignmentColumns([]string{"code", "short_name", "code", "addr", "contact",
			"telephone", "short_code", "delivery_addr", "route_id", "route_name", "status", "trustee_id", "trustee_name"}),
	}).Create(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *ThmsMapper) Update(m *web_thms_model.YtlThms) (ret *web_thms_model.YtlThms, err error) {
	mapper.Lock.Lock()
	defer mapper.Lock.Unlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_thms_model.YtlThms{}).Where("id = ?", m.ID).Updates(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *ThmsMapper) CreateOrUpdate(m *web_thms_model.YtlThms) (ret *web_thms_model.YtlThms, err error) {
	mapper.Lock.Lock()
	defer mapper.Lock.Unlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Model(m).Save(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *ThmsMapper) QueryById(id int64) (m *web_thms_model.YtlThms, err error) {
	mapper.Lock.RLock()
	defer mapper.Lock.RUnlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_thms_model.YtlThms{}).Where("id = ?", id).First(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *ThmsMapper) QueryByCode(code string) (m *web_thms_model.YtlThms, err error) {
	mapper.Lock.RLock()
	defer mapper.Lock.RUnlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_thms_model.YtlThms{}).Where("code = ?", code).First(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *ThmsMapper) QueryByIds(ids []int64) (m []*web_thms_model.YtlThms, err error) {
	mapper.Lock.RLock()
	defer mapper.Lock.RUnlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_thms_model.YtlThms{}).Where("id IN ?", ids).Find(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *ThmsMapper) DeleteById(id int64) (err error) {
	mapper.Lock.Lock()
	defer mapper.Lock.Unlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Unscoped().Where("id = ?", id).Delete(&web_thms_model.YtlThms{}).Error; err != nil {
		return err
	}
	return nil
}

func (mapper *ThmsMapper) DeleteByIds(ids []int64) (err error) {
	mapper.Lock.Lock()
	defer mapper.Lock.Unlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Unscoped().Where("id IN ?", ids).Delete(&web_thms_model.YtlThms{}).Error; err != nil {
		return err
	}
	return nil
}

func (mapper *ThmsMapper) Page(pageIndex, pageSize int, customerId, deviceId, ttIndex, alarmType string) (items []*web_vo.ThmsPageRet, count int64, err error) {
	mapper.Lock.RLock()
	defer mapper.Lock.RUnlock()
	var countTmp int64 = 0
	table := mapper.DB.WithContext(mapper.Ctx).Table("ytl_thms").Select("ytl_thms.*,strftime('%Y-%m-%d %H:%M:%f',created_at,'localtime') as f_created_at")
	if customerId != "" {
		table = table.Where("customer_id like ?", "%"+customerId+"%")
	}
	if deviceId != "" {
		table = table.Where("device_id like ?", "%"+deviceId+"%")
	}
	if ttIndex != "" {
		table = table.Where("tt_index like ?", "%"+ttIndex+"%")
	}
	if alarmType != "" {
		switch alarmType {
		case "0":
			table = table.Where("flag = ?", "0")
			break
		case "1":
			table = table.Where("(flag & 0x01) = ?", 1)
			break
		case "2":
			table = table.Where("(flag & 0x02) = ?", 2)
			break
		case "4":
			table = table.Where("(flag & 0x04) = ?", 4)
			break
		case "8":
			table = table.Where("(flag & 0x08) = ?", 8)
			break
		}
	}
	if err := table.Order("created_at desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&items).Offset(-1).Limit(-1).Count(&countTmp).Error; err != nil {
		return nil, 0, err
	}
	return items, countTmp, nil
}

func (mapper *ThmsMapper) List() (items []*web_thms_model.YtlThms, count int64, err error) {
	mapper.Lock.RLock()
	defer mapper.Lock.RUnlock()
	var countTmp int64 = 0
	table := mapper.DB.WithContext(mapper.Ctx)
	if err := table.Find(&items).Offset(-1).Limit(-1).Count(&countTmp).Error; err != nil {
		return nil, 0, err
	}
	return items, countTmp, nil
}
